package com.ruoyi.activiti.listener;

import com.alibaba.fastjson.JSON;
import com.ruoyi.activiti.bpmnparse.x6translator.MultiUserTaskTranslator;
import com.ruoyi.activiti.bpmnparse.x6translator.X6Helper;
import com.ruoyi.activiti.bpmnparse.x6translator.X6UserTaskTranslator;
import com.ruoyi.activiti.domain.CustomProcess;
import com.ruoyi.activiti.mapper.CustomProcessMapperHis;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会签任务监听器，当会签任务完成时统计同意的数量
 *
 * @author henryyan
 */
@Component
public class CounterSignCompleteListener implements TaskListener {

    private static final long serialVersionUID = 1L;

    @Autowired
    private CustomProcessMapperHis customProcessMapperHis;
    @Override
    public void notify(DelegateTask delegateTask) {
        Boolean pass = (Boolean) delegateTask.getVariable("pass");
        if (pass) {
            Long agreeCounter = (Long) delegateTask.getVariable("approvedCounter");
            if(agreeCounter == null) {
                agreeCounter = 0L;
            }
            agreeCounter = agreeCounter + 1;
            delegateTask.setVariable("approvedCounter", agreeCounter);
            setPassVariable(delegateTask,agreeCounter);
        } else {
            Long rejectedCounter = (Long) delegateTask.getVariable("rejectedCounter");
            if(rejectedCounter == null) {
                rejectedCounter = 0L;
            }
            rejectedCounter = rejectedCounter + 1;
            delegateTask.setVariable("rejectedCounter", rejectedCounter);
            setRejectVariable(delegateTask,rejectedCounter);
        }
    }
    private void setPassVariable(DelegateTask delegateTask,Long agreeCounter){
        Integer nrOfInstances = (Integer) delegateTask.getExecution().getVariable("nrOfInstances");
        String procDefId = delegateTask.getProcessDefinitionId();
        CustomProcess customProcess = customProcessMapperHis.selectByProcDefId(procDefId);
        String x6GraphData = customProcess.getGraphData();
        Map<String,Object> x6Map = JSON.parseObject(x6GraphData);
        List<Map<String,Object>> listMap = (List<Map<String, Object>>) x6Map.get("cells");
        X6Helper x6Helper = new X6Helper();
        Map<String,Object> dataMap = x6Helper.getBpmnDataById(listMap,delegateTask.getTaskDefinitionKey());
        String approveValue = (String) dataMap.get("approvedValue");
        String approveStrategy = (String) dataMap.get("approvedStrategy");
        if(MultiUserTaskTranslator.STRATEGY_COUNT.equals(approveStrategy)) {
            Integer approveInt = Integer.valueOf(approveValue);
            if(agreeCounter >= approveInt) {
                delegateTask.setVariable("pass",true);
            }
        } else if(MultiUserTaskTranslator.STRATEGY_PERCENT.equals(approveStrategy)) {
            Float approveInt = Float.valueOf(approveValue);
            float result = agreeCounter * 1.0f / nrOfInstances;
            if(result >= approveInt) {
                delegateTask.setVariable("pass",true);
            }
        }
    }
    private void setRejectVariable(DelegateTask delegateTask,Long rejectCounter){
        Integer nrOfInstances = (Integer) delegateTask.getExecution().getVariable("nrOfInstances");
        String procDefId = delegateTask.getProcessDefinitionId();
        CustomProcess customProcess = customProcessMapperHis.selectByProcDefId(procDefId);
        String x6GraphData = customProcess.getGraphData();
        Map<String,Object> x6Map = JSON.parseObject(x6GraphData);
        List<Map<String,Object>> listMap = (List<Map<String, Object>>) x6Map.get("cells");
        X6Helper x6Helper = new X6Helper();
        Map<String,Object> dataMap = x6Helper.getBpmnDataById(listMap,delegateTask.getTaskDefinitionKey());
        String rejectValue = (String) dataMap.get("rejectValue");
        String rejectStrategy = (String) dataMap.get("rejectStrategy");
        if(MultiUserTaskTranslator.STRATEGY_COUNT.equals(rejectStrategy)) {
            Integer rejectInt = Integer.valueOf(rejectValue);
            if(rejectCounter >= rejectInt) {
                delegateTask.setVariable("pass",false);
            }
        } else if(MultiUserTaskTranslator.STRATEGY_PERCENT.equals(rejectStrategy)) {
            Float rejectInt = Float.valueOf(rejectValue);
            float result = rejectCounter * 1.0f / nrOfInstances;
            if(result >= rejectInt) {
                delegateTask.setVariable("pass",false);
            }
        }
    }

}
